package com.kevin.servlet3;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "AsyncTest", urlPatterns = "/AsyncTest", asyncSupported = true)
public class AsyncTest extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(javax.servlet.http.HttpServletRequest req,
			javax.servlet.http.HttpServletResponse resp)
			throws javax.servlet.ServletException, java.io.IOException {

		resp.setContentType("text/html");

		PrintWriter out = resp.getWriter();

		out.println("severlet start:" + new Date());
		out.flush();

		AsyncContext asyncContext = req.startAsync();

		asyncContext.addListener(new MyListener());

		Thread thread = new MyThread(asyncContext);
		thread.start();

		out.println("severlet end:" + new Date());
		out.flush();
	};

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
}
